package com.groupbuying.store.controller.forInternal;

import com.groupbuying.store.domain.StoreAccountWxinfoRelDO;
import com.groupbuying.store.service.StoreAccountWxinfoRelService;
import com.groupbuying.store.utils.PageUtils;
import com.groupbuying.store.utils.Query;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 商户微信登录信息关联表
 *
 * @author gaoruijun
 * @email 18511893135@163.com
 * @date 2019-07-12 11:48:19
 */

@RestController
@RequestMapping("/storeAccountWxinfoRel")
public class StoreAccountWxinfoRelController {
    public static final Logger logger = LoggerFactory.getLogger(StoreAccountWxinfoRelController.class);
    @Autowired
    private StoreAccountWxinfoRelService storeAccountWxinfoRelService;

    @GetMapping()
    String StoreAccountWxinfoRel() {
        return "store/storeAccountWxinfoRel/storeAccountWxinfoRel";
    }

    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<StoreAccountWxinfoRelDO> storeAccountWxinfoRelList = storeAccountWxinfoRelService.list(query);
        int total = storeAccountWxinfoRelService.count(query);
        PageUtils pageUtils = new PageUtils(storeAccountWxinfoRelList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add() {
        return "store/storeAccountWxinfoRel/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") BigInteger id, Model model) {
        StoreAccountWxinfoRelDO storeAccountWxinfoRel = storeAccountWxinfoRelService.get(id);
        model.addAttribute("storeAccountWxinfoRel", storeAccountWxinfoRel);
        return "store/storeAccountWxinfoRel/edit";
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(StoreAccountWxinfoRelDO storeAccountWxinfoRel) {
        if (storeAccountWxinfoRelService.save(storeAccountWxinfoRel) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(StoreAccountWxinfoRelDO storeAccountWxinfoRel) {
        storeAccountWxinfoRelService.update(storeAccountWxinfoRel);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    public R remove(BigInteger id) {
        if (storeAccountWxinfoRelService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 商户信息和openId绑定落库
     *
     * @param param
     * @return
     */
    @PostMapping("/loginStoreAccountWxInfo")
    public ApiResult saveStoreAccountWxInfo(@RequestParam Map<String, Object> param) {
        return storeAccountWxinfoRelService.getByAccountAndOpen(param);
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    public R remove(@RequestParam("ids[]") BigInteger[] ids) {
        storeAccountWxinfoRelService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 退出登陆
     *
     * @param parma
     * @return
     */
    @PostMapping("/loginOut")
    public R loginOut(@RequestParam Map<String, String> parma) {
        logger.info("退出登陆的参数为parma:{}", parma);
        storeAccountWxinfoRelService.removeByAccountIdAndOpenId(parma);
        return R.ok();

    }

    @GetMapping("/getOpenIds")
    public Map<String, Set<String>> getOpenIds(String counterId) {

        return storeAccountWxinfoRelService.getOpenAndAccountByCounterId(counterId);
    }

    /**
     * 通过手机号删除微信用户登陆关联信息(目前没用，预备使用)
     * @param mobile
     * @return
     */
    @PostMapping("/loginOutByMobile")
    public R loginOutByMobile(@RequestParam("mobile") String mobile) {
        logger.info("parma:{}", mobile);
        storeAccountWxinfoRelService.removeByMobile(mobile);
        return R.ok();

    }
    @PostMapping("/loginOutByOpenId")
    public ApiResult loginOutByOpenId(@RequestBody String code){
        logger.info("接收到的code为:{}",code);
        return storeAccountWxinfoRelService.deleteByOpenId(code);
    }
}
